﻿using System;
using DNT.RavenQA.Core.Extensions;
using DNT.RavenQA.Core.Models;
using Raven.Client;

namespace DNT.RavenQA.Web.Infrastructure.Commands
{
    public class VoteUpCommand<TContribute> : VoteCommand where TContribute : Contribute
    {
        public VoteUpCommand(Input parameter)
            : base(parameter)
        {
        }

        public override CommandResult<Result> Execute(IDocumentSession session)
        {
            return Execute(session, (contribute, builder, user) => contribute.VoteUp(builder, user));
        }

        protected override Contribute GetContribute(IDocumentSession session)
        {
            Contribute contribute = session.Include<TContribute>(x => x.VotesId)
                                           .Load<TContribute>(Parameter.ContributeId);

            if (contribute.IsNull())
                throw new NonExistingContributeException();

            return contribute;
        }
    }
}